package cn.com.dhcc.turbo.service.rmi;

import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.handler.remotehandler.rmi.RmiServerCenter;
import cn.com.dhcc.app.core.handler.remotehandler.socket.ICallBack;
import cn.com.dhcc.app.core.handler.remotehandler.socket.ISendDataClient;
import cn.com.dhcc.app.core.tag.CommonStaticCode.SUCCESSFAIL;
import cn.com.dhcc.app.core.util.NetUtil;
import cn.com.dhcc.app.pub.core.consts.AppIocBean;
import cn.com.dhcc.app.pub.core.listener.RmiServerInitListener.RMISERICE_NAMESPACE;
import cn.com.dhcc.turbo.service.registry.sync.ISynchronizeHandler;
import cn.com.dhcc.turbo.service.registry.sync.SyncCallBack;
/**
 * rmi 数据同步
 * @author qiao
 * @2014-9-1 @下午2:31:23
 */
@Service(AppIocBean.RMI_SENDMESSAGE_CLIENT)
public class RmiSendMessageClient implements ISendDataClient{

	ISynchronizeHandler iSynchronizeHandler = null;
	@Override
	public void sendData(String ip, int port, Object dataToSend,
			ICallBack iCallBack) throws Exception {
		boolean isActive = NetUtil.canConnect(ip, port);
		if(isActive){
			iSynchronizeHandler = RmiServerCenter.connectServer(ip, port, RMISERICE_NAMESPACE.RMIMESSAGE.name());
			SyncCallBack callBack = iSynchronizeHandler.receiveData(dataToSend);
			iCallBack.recevieCallBackData(callBack);
		}else{
			SyncCallBack callBack = new SyncCallBack();
			callBack.setResult(SUCCESSFAIL.FAIL);
			callBack.setMsg("操作失败，信息同步地址["+ip+":"+port+"]连接失败!");
			iCallBack.recevieCallBackData(callBack);
		}
	}
}
